﻿using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace WebTimer.Common
{
    public class Utils
    {
        /// <summary>
        /// 设置应用程序开机自动运行
        /// </summary>
        /// <param name="fileName">应用程序的文件名</param>
        /// <param name="isAutoRun">是否自动运行,为false时，取消自动运行</param>
        /// <exception cref="system.Exception">设置不成功时抛出异常</exception>
        /// <returns>返回1成功，非1不成功</returns>
        public static String SetAutoRun(string fileName, bool isAutoRun)
        {
            string reSet = string.Empty;
            RegistryKey reg = null;
            try
            {
                if (!System.IO.File.Exists(fileName))
                {
                    reSet = "该文件不存在!";
                }
                string name = fileName.Substring(fileName.LastIndexOf(@"\") + 1);
                reg = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true);
                if (reg == null)
                {
                    reg = Registry.LocalMachine.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");
                }
                if (isAutoRun)
                {
                    reg.SetValue(name, fileName);
                    reSet = "1";
                }
                else
                {
                    reg.SetValue(name, false);
                }

            }
            catch (Exception ex)
            {
                //“记录异常”
            }
            finally
            {
                if (reg != null)
                {
                    reg.Close();
                }
            }
            return reSet;
        }

        /// <summary>
        /// 判断两个数据流是否一致
        /// </summary>
        /// <param name="filePath1"></param>
        /// <param name="filePath2"></param>
        /// <returns></returns>
        public static bool IsValidStreamContent(Stream filePath1, Stream filePath2)
        {
            //创建一个哈希算法对象 
            using (HashAlgorithm hash = HashAlgorithm.Create())
            {
                byte[] hashByte1 = hash.ComputeHash(filePath1);//哈希算法根据文本得到哈希码的字节数组 
                byte[] hashByte2 = hash.ComputeHash(filePath2);
                string str1 = BitConverter.ToString(hashByte1);//将字节数组装换为字符串 
                string str2 = BitConverter.ToString(hashByte2);
                return (str1 == str2);//比较哈希码 
            }
        }

        /// <summary>
        /// 发送邮件
        /// </summary>
        /// <param name="username">要发送的邮件列表</param>
        /// <param name="subject">邮件标题</param>
        /// <param name="body">邮件内容</param>
        /// <returns></returns>
        public static bool SendMail(string subject, string body, string to)
        {
            string[] username = to.Split(';');

            WebTimer.Mail.SysMailMessage sysMsg = new WebTimer.Mail.SysMailMessage();
            sysMsg.AddRecipient(username);

            sysMsg.Subject = subject;
            sysMsg.Body = body;
            sysMsg.From = ConfigurationManager.AppSettings["MailAddress"];
            sysMsg.Html = true;
            sysMsg.MailDomain = ConfigurationManager.AppSettings["Host"];
            sysMsg.MailDomainPort = Convert.ToInt32(ConfigurationManager.AppSettings["Port"]);
            sysMsg.MailServerUserName = ConfigurationManager.AppSettings["MailUserName"];
            sysMsg.MailServerPassWord = ConfigurationManager.AppSettings["MailUserPwd"];
            return sysMsg.Send();
        }
    }
}
